package com.qianhui.dao;

import com.qianhui.common.IResult;
import com.qianhui.pojo.ProvincePostage;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * A data access object (DAO) providing persistence and search support for
 * ProvincePostage entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 *
 * @author MyEclipse Persistence Tools
 * @see com.qianhui.pojo.ProvincePostage
 */
public class ProvincePostageDAO extends BaseHibernateDAO
{
    private static final Logger log = LoggerFactory.getLogger(ProvincePostageDAO.class);
    // property constants
    public static final String PROVINCE_NAME = "provinceName";
    public static final String PROVINCE_POSTAGE = "provincePostage";

    public ProvincePostage save(ProvincePostage transientInstance)
    {
        Transaction tx = null;
        try
        {
            tx = getSession().beginTransaction();
            getSession().saveOrUpdate(transientInstance);
            tx.commit();
        } catch (HibernateException e)
        {
            e.printStackTrace();
            if (tx != null)
            {
                tx.rollback();
            }
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
        return transientInstance;
    }

    public List<ProvincePostage> saveList(List<ProvincePostage> list)
    {
        Transaction tx = null;
        try
        {
            tx = getSession().beginTransaction();
            for (ProvincePostage temp : list)
            {
                getSession().saveOrUpdate(temp);
            }
            tx.commit();
        } catch (HibernateException e)
        {
            e.printStackTrace();
            if (tx != null)
            {
                tx.rollback();
            }
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
        return list;
    }

    public int delete(ProvincePostage persistentInstance)
    {
        Transaction tx = null;
        try
        {
            tx = getSession().beginTransaction();
            getSession().delete(persistentInstance);
            tx.commit();
        } catch (HibernateException e)
        {
            e.printStackTrace();
            if (tx != null)
            {
                tx.rollback();
            }
            return IResult.DELETE_EXCEPTION;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
        return IResult.DELETE_SUCCESS;
    }

    public ProvincePostage findById(java.lang.String id)
    {
        log.debug("getting ProvincePostage instance with id: " + id);
        try
        {
            ProvincePostage instance = (ProvincePostage) getSession().get("com.qianhui.pojo.ProvincePostage", id);
            return instance;
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public List findByProperty(String propertyName, Object value)
    {
        log.debug("finding ProvincePostage instance with property: " + propertyName + ", value: " + value);
        try
        {
            String queryString = "from ProvincePostage as model where model." + propertyName + "= ?";
            Query queryObject = getSession().createQuery(queryString);
            queryObject.setParameter(0, value);
            return queryObject.list();
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public List findByProvinceName(Object provinceName)
    {
        return findByProperty(PROVINCE_NAME, provinceName);
    }

    public List findByProvincePostage(Object provincePostage)
    {
        return findByProperty(PROVINCE_POSTAGE, provincePostage);
    }

    public List findAll()
    {
        log.debug("finding all ProvincePostage instances");
        try
        {
            String queryString = "from ProvincePostage";
            Query queryObject = getSession().createQuery(queryString);
            return queryObject.list();
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

}